from django.shortcuts import render, redirect
from course.models import teacher_manage
from course import models
from course import page

# Create your views here.
def index(request):
    #分页相关
    current_page = request.GET.get('page',1)
    page_obj = page.Pager(current_page)
    # all_item = models.teacher_manage.objects.all().count()
    # pager_str = page_obj.page_str(all_item,'/teacher/')


    # 教师信息,前面获取一次，如果有值（主要是搜索栏搜索的时候会传值，默认为空格，即所有）
    #尝试做成一个搜索框对应多个属性
    string = request.GET.get('name', '')

    if string.strip() != '':
        #判断搜索框是否存在值

        if string.isdigit():
            # 判断是否是纯数字,纯数字匹配id
            teachers = teacher_manage.objects.filter(teacher_id__contains=string).order_by("-update_time")[page_obj.start:page_obj.end]

            all_item = teacher_manage.objects.filter(teacher_id__contains=string).order_by("-update_time").count()
            pager_str = page_obj.page_str(all_item, '/teacher/?name='+string)
            #把string进行传递，确保搜索后的翻页正常。

            #这里的跳转之后，路径在page页面生成的有问题。。base_page那个
            return render(request, 'teacher/index.html', {'teachers': teachers, 'pager_str':pager_str})
        else:
            teachers = teacher_manage.objects.filter(name__contains=string).order_by("-update_time")[page_obj.start:page_obj.end]

            all_item = teacher_manage.objects.filter(name__contains=string).order_by("-update_time").count()
            pager_str = page_obj.page_str(all_item, '/teacher/?name='+string)

            return render(request, 'teacher/index.html', {'teachers': teachers,'pager_str':pager_str})
    else:#不存在则默认输出所有数据
        teachers = teacher_manage.objects.all().order_by("-update_time")[page_obj.start:page_obj.end]
        #print(teachers)

        all_item = models.teacher_manage.objects.all().order_by("-update_time").count()
        pager_str2 = page_obj.page_str2(all_item, '/teacher/')

        return render(request, 'teacher/index.html', {'teachers': teachers,'pager_str':pager_str2})

def add(request):
    if request.method == 'GET':
        return render(request, 'teacher/add.html')
    else:
        name = request.POST.get('name', '')
        tel = request.POST.get('tel', '')

        test1 = teacher_manage(name=name, tel=tel)  # 往输入库里面插入了一条，name=runoob，id自动生成。
        test1.save()
        return redirect('../')

# 教师信息修改处理函数
def edit(request):
    if request.method == 'GET':
        teacher_id = request.GET.get("teacher_id")
        teacher = teacher_manage.objects.get(teacher_id=teacher_id)
        return render(request, 'teacher/edit.html', {'teacher':teacher})
    else:
        teacher_id = request.POST.get('teacher_id')
        name = request.POST.get('name', '')
        tel = request.POST.get('tel', '')

        teacher_manage.objects.filter(teacher_id=teacher_id).update(name=name, tel=tel)
        return redirect('../')

def delete(request):
    teacher_id = request.GET.get("teacher_id")

    test1 = teacher_manage.objects.get(teacher_id=teacher_id)
    test1.delete()
    return redirect('../')
